package avicit.bdp.dcs.json.service.writer;

import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dcs.datasource.dto.JobDatasource;
import avicit.bdp.dcs.job.dto.JobDTO;
import avicit.bdp.dcs.plugin.BaseDataxParam;
import avicit.bdp.dcs.task.dto.StreamColumnMappingDto;
import avicit.bdp.dcs.task.dto.TaskDTO;
import avicit.bdp.dcs.utils.JdbcConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StreamWriterBuilder {

  @Autowired private IotdbWriterBuilder iotdbWriterBuilder;
  @Autowired private HiveWriterBuilder hiveWriterBuilder;

  /** 构建流式采集Writer */
  public BaseDataxParam buildWriterByStream(
      TaskDTO taskDTO, JobDTO jobDTO, JobDatasource writerDatasource) throws Exception {
    BaseDataxParam dataxParam = null;
    // 构建Writer
    List<StreamColumnMappingDto> streamColumnMappingDtoList =
        JSONUtils.toList(taskDTO.getMappingColumn(), StreamColumnMappingDto.class);

    if (JdbcConstants.IOTDB.equals(writerDatasource.getDbType())) {
      // 构建IotdbWriter
      dataxParam =
          iotdbWriterBuilder.buildIotdbWriterByStream(
              jobDTO, streamColumnMappingDtoList, writerDatasource);
    } else if (JdbcConstants.HIVE.equals(writerDatasource.getDbType())) {
      // 构建HiveWriter
      dataxParam =
          hiveWriterBuilder.buildHiveWriterByStream(
              jobDTO, streamColumnMappingDtoList, writerDatasource);
    }

    return dataxParam;
  }
}
